package com.cswnode.demo.controller;

import com.cswnode.demo.entity.Comment;
import com.cswnode.demo.entity.Reply;
import com.cswnode.demo.service.CommentService;
import com.cswnode.demo.service.ReplyService;
import com.cswnode.demo.util.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Comment)表控制层
 *
 * @author makejava
 * @since 2021-04-01 19:00:47
 */
@Api(tags = "评论管理")
@RestController
@RequestMapping("/comment")
public class CommentController {
    /**
     * 服务对象
     */
    @Resource
    private CommentService commentService;
    @Autowired
    private ReplyService replyService;

    @ApiOperation(value = "获得当前岗位的所有评论")
    @GetMapping("/get/{postId}")
    public ResponseVo get(@PathVariable("postId") Long postId){
        List<Comment> comments = commentService.queryByPostId(postId);
        return ResponseVo.success(comments);
    }

    @ApiOperation(value = "插入父评论")
    @PostMapping("/addComment")
    public ResponseVo addComment(@RequestBody Comment comment){
        if (comment == null){
            return ResponseVo.error("评论失败！");
        }
        commentService.insert(comment);
        return ResponseVo.success("评论成功！");
    }

    @ApiOperation(value = "插入子评论")
    @PostMapping("/addReply")
    public ResponseVo addReply(@RequestBody Reply reply){
        if (reply == null){
            return ResponseVo.error("评论失败！");
        }
        replyService.insert(reply);
        return ResponseVo.success("评论成功！");
    }


}